home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 1: Comms & Networking / Almathera Ten on Ten - Disc 1: Comms & Networking.iso / amiga-useful / perl / src / regexec.c < prev    next >
C/C++ Source or Header  |  1995-05-04  |  21KB  |  912 lines

  1. /* NOTE: this is derived from Henry Spencer's regexp code, and should not
  2.  * confused with the original package (see point 3 below).  Thanks, Henry!
  3.  */
  4.  
  5. /* Additional note: this code is very heavily munged from Henry's version
  6.  * in places.  In some spots I've traded clarity for efficiency, so don't
  7.  * blame Henry for some of the lack of readability.
  8.  */
  9.  
  10. /* $RCSfile: regexec.c,v $$Revision: 4.0.1.4 $$Date: 92/06/08 15:25:50 $
  11.  *
  12.  * $Log:    regexec.c,v $
  13.  * Revision 4.0.1.4  92/06/08  15:25:50  lwall
  14.  * patch20: pattern modifiers i and g didn't interact right
  15.  * patch20: in some cases $` and $' didn't get set by match
  16.  * patch20: /x{0}/ was wrongly interpreted as /x{0,}/
  17.  * 
  18.  * Revision 4.0.1.3  91/11/05  18:23:55  lwall
  19.  * patch11: prepared for ctype implementations that don't define isascii()
  20.  * patch11: initial .* in pattern had dependency on value of $*
  21.  * 
  22.  * Revision 4.0.1.2  91/06/07  11:50:33  lwall
  23.  * patch4: new copyright notice
  24.  * patch4: // wouldn't use previous pattern if it started with a null character
  25.  * 
  26.  * Revision 4.0.1.1  91/04/12  09:07:39  lwall
  27.  * patch1: regexec only allocated space for 9 subexpresssions
  28.  * 
  29.  * Revision 4.0  91/03/20  01:39:16  lwall
  30.  * 4.0 baseline.
  31.  * 
  32.  */
  33. /*SUPPRESS 112*/
  34. /*
  35.  * regcomp and regexec -- regsub and regerror are not used in perl
  36.  *
  37.  *    Copyright (c) 1986 by University of Toronto.
  38.  *    Written by Henry Spencer.  Not derived from licensed software.
  39.  *
  40.  *    Permission is granted to anyone to use this software for any
  41.  *    purpose on any computer system, and to redistribute it freely,
  42.  *    subject to the following restrictions:
  43.  *
  44.  *    1. The author is not responsible for the consequences of use of
  45.  *        this software, no matter how awful, even if they arise
  46.  *        from defects in it.
  47.  *
  48.  *    2. The origin of this software must not be misrepresented, either
  49.  *        by explicit claim or by omission.
  50.  *
  51.  *    3. Altered versions must be plainly marked as such, and must not
  52.  *        be misrepresented as being the original software.
  53.  *
  54.  ****    Alterations to Henry's code are...
  55.  ****
  56.  ****    Copyright (c) 1991, Larry Wall
  57.  ****
  58.  ****    You may distribute under the terms of either the GNU General Public
  59.  ****    License or the Artistic License, as specified in the README file.
  60.  *
  61.  * Beware that some of this code is subtly aware of the way operator
  62.  * precedence is structured in regular expressions.  Serious changes in
  63.  * regular-expression syntax might require a total rethink.
  64.  */
  65. #include "EXTERN.h"
  66. #include "perl.h"
  67. #include "regcomp.h"
  68.  
  69. #ifndef STATIC
  70. #define    STATIC    static
  71. #endif
  72.  
  73. #ifdef DEBUGGING
  74. int regnarrate = 0;
  75. #endif
  76.  
  77. /*
  78.  * regexec and friends
  79.  */
  80.  
  81. /*
  82.  * Global work variables for regexec().
  83.  */
  84. static char *regprecomp;
  85. static char *reginput;        /* String-input pointer. */
  86. static char regprev;        /* char before regbol, \n if none */
  87. static char *regbol;        /* Beginning of input, for ^ check. */
  88. static char *regeol;        /* End of input, for $ check. */
  89. static char **regstartp;    /* Pointer to startp array. */
  90. static char **regendp;        /* Ditto for endp. */
  91. static char *reglastparen;    /* Similarly for lastparen. */
  92. static char *regtill;
  93.  
  94. static int regmyp_size = 0;
  95. static char **regmystartp = Null(char**);
  96. static char **regmyendp   = Null(char**);
  97.  
  98. /*
  99.  * Forwards.
  100.  */
  101. STATIC int regtry();
  102. STATIC int regmatch();
  103. STATIC int regrepeat();
  104.  
  105. extern int multiline;
  106.  
  107. /*
  108.  - regexec - match a regexp against a string
  109.  */
  110. int
  111. regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
  112. register regexp *prog;
  113. char *stringarg;
  114. register char *strend;    /* pointer to null at end of string */
  115. char *strbeg;    /* real beginning of string */
  116. int minend;    /* end of match must be at least minend after stringarg */
  117. STR *screamer;
  118. int safebase;    /* no need to remember string in subbase */
  119. {
  120.     register char *s;
  121.     register int i;
  122.     register char *c;
  123.     register char *string = stringarg;
  124.     register int tmp;
  125.     int minlen = 0;        /* must match at least this many chars */
  126.     int dontbother = 0;    /* how many characters not to try at end */
  127.  
  128.     /* Be paranoid... */
  129.     if (prog == NULL || string == NULL) {
  130.         fatal("NULL regexp parameter");
  131.         return(0);
  132.     }
  133.  
  134.     if (string == strbeg)    /* is ^ valid at stringarg? */
  135.         regprev = '\n';
  136.     else {
  137.         regprev = stringarg[-1];
  138.         if (!multiline && regprev == '\n')
  139.         regprev = '\0';        /* force ^ to NOT match */
  140.     }
  141.     regprecomp = prog->precomp;
  142.     /* Check validity of program. */
  143. #ifdef AMIGA
  144.     if (((unsigned char)(prog->program[0])) != (unsigned char)MAGIC) {
  145. #else
  146.     if (UCHARAT(prog->program) != MAGIC) {
  147. #endif
  148.         FAIL("corrupted regexp program");
  149.     }
  150.  
  151.     if (prog->do_folding) {
  152.         i = strend - string;
  153.         New(1101,c,i+1,char);
  154.         Copy(string, c, i+1, char);
  155.         string = c;
  156.         strend = string + i;
  157.         for (s = string; s < strend; s++)
  158.             if (isUPPER(*s))
  159.                 *s = tolower(*s);
  160.     }
  161.  
  162.     /* If there is a "must appear" string, look for it. */
  163.     s = string;
  164.     if (prog->regmust != Nullstr &&
  165.         (!(prog->reganch & ROPT_ANCH)
  166.          || (multiline && prog->regback >= 0)) ) {
  167.         if (stringarg == strbeg && screamer) {
  168.             if (screamfirst[prog->regmust->str_rare] >= 0)
  169.                 s = screaminstr(screamer,prog->regmust);
  170.             else
  171.                 s = Nullch;
  172.         }
  173. #ifndef lint
  174.         else
  175.             s = fbminstr((unsigned char*)s, (unsigned char*)strend,
  176.                 prog->regmust);
  177. #endif
  178.         if (!s) {
  179.             ++prog->regmust->str_u.str_useful;    /* hooray */
  180.             goto phooey;    /* not present */
  181.         }
  182.         else if (prog->regback >= 0) {
  183.             s -= prog->regback;
  184.             if (s < string)
  185.                 s = string;
  186.             minlen = prog->regback + prog->regmust->str_cur;
  187.         }
  188.         else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
  189.             str_free(prog->regmust);
  190.             prog->regmust = Nullstr;    /* disable regmust */
  191.             s = string;
  192.         }
  193.         else {
  194.             s = string;
  195.             minlen = prog->regmust->str_cur;
  196.         }
  197.     }
  198.  
  199.     /* Mark beginning of line for ^ . */
  200.     regbol = string;
  201.  
  202.     /* Mark end of line for $ (and such) */
  203.     regeol = strend;
  204.  
  205.     /* see how far we have to get to not match where we matched before */
  206.     regtill = string+minend;
  207.  
  208.     /* Allocate our backreference arrays */
  209.     if ( regmyp_size < prog->nparens + 1 ) {
  210.         /* Allocate or enlarge the arrays */
  211.         regmyp_size = prog->nparens + 1;
  212.         if ( regmyp_size < 10 ) regmyp_size = 10;    /* minimum */
  213.         if ( regmystartp ) {
  214.         /* reallocate larger */
  215.         Renew(regmystartp,regmyp_size,char*);
  216.         Renew(regmyendp,  regmyp_size,char*);
  217.         }
  218.         else {
  219.         /* Initial allocation */
  220.         New(1102,regmystartp,regmyp_size,char*);
  221.         New(1102,regmyendp,  regmyp_size,char*);
  222.         }
  223.     
  224.     }
  225.  
  226.     /* Simplest case:  anchored match need be tried only once. */
  227.     /*  [unless multiline is set] */
  228.     if (prog->reganch & ROPT_ANCH) {
  229.         if (regtry(prog, string))
  230.             goto got_it;
  231.         else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
  232.             if (minlen)
  233.                 dontbother = minlen - 1;
  234.             strend -= dontbother;
  235.             /* for multiline we only have to try after newlines */
  236.             if (s > string)
  237.                 s--;
  238.             while (s < strend) {
  239.                 if (*s++ == '\n') {
  240.                 if (s < strend && regtry(prog, s))
  241.                     goto got_it;
  242.                 }
  243.             }
  244.         }
  245.         goto phooey;
  246.     }
  247.  
  248.     /* Messy cases:  unanchored match. */
  249.     if (prog->regstart) {
  250.         if (prog->reganch & ROPT_SKIP) {  /* we have /x+whatever/ */
  251.             /* it must be a one character string */
  252.             i = prog->regstart->str_ptr[0];
  253.             while (s < strend) {
  254.                 if (*s == i) {
  255.                     if (regtry(prog, s))
  256.                         goto got_it;
  257.                     s++;
  258.                     while (s < strend && *s == i)
  259.                     s++;
  260.                 }
  261.                 s++;
  262.             }
  263.         }
  264.         else if (prog->regstart->str_pok == 3) {
  265.             /* We know what string it must start with. */
  266. #ifndef lint
  267.             while ((s = fbminstr((unsigned char*)s,
  268.               (unsigned char*)strend, prog->regstart)) != NULL)
  269. #else
  270.             while (s = Nullch)
  271. #endif
  272.             {
  273.                 if (regtry(prog, s))
  274.                     goto got_it;
  275.                 s++;
  276.             }
  277.         }
  278.         else {
  279.             c = prog->regstart->str_ptr;
  280.             while ((s = ninstr(s, strend,
  281.               c, c + prog->regstart->str_cur )) != NULL) {
  282.                 if (regtry(prog, s))
  283.                     goto got_it;
  284.                 s++;
  285.             }
  286.         }
  287.         goto phooey;
  288.     }
  289.     /*SUPPRESS 560*/
  290.     if (c = prog->regstclass) {
  291.         int doevery = (prog->reganch & ROPT_SKIP) == 0;
  292.  
  293.         if (minlen)
  294.             dontbother = minlen - 1;
  295.         strend -= dontbother;    /* don't bother with what can't match */
  296.         tmp = 1;
  297.         /* We know what class it must start with. */
  298.         switch (OP(c)) {
  299.         case ANYOF:
  300.             c = OPERAND(c);
  301.             while (s < strend) {
  302.                 i = UCHARAT(s);
  303.                 if (!(c[i >> 3] & (1 << (i&7)))) {
  304.                     if (tmp && regtry(prog, s))
  305.                         goto got_it;
  306.                     else
  307.                         tmp = doevery;
  308.                 }
  309.                 else
  310.                     tmp = 1;
  311.                 s++;
  312.             }
  313.             break;
  314.         case BOUND:
  315.             if (minlen)
  316.             dontbother++,strend--;
  317.             if (s != string) {
  318.             i = s[-1];
  319.             tmp = isALNUM(i);
  320.             }
  321.             else
  322.             tmp = isALNUM(regprev);    /* assume not alphanumeric */
  323.             while (s < strend) {
  324.                 i = *s;
  325.                 if (tmp != isALNUM(i)) {
  326.                     tmp = !tmp;
  327.                     if (regtry(prog, s))
  328.                         goto got_it;
  329.                 }
  330.                 s++;
  331.             }
  332.             if ((minlen || tmp) && regtry(prog,s))
  333.                 goto got_it;
  334.             break;
  335.         case NBOUND:
  336.             if (minlen)
  337.             dontbother++,strend--;
  338.             if (s != string) {
  339.             i = s[-1];
  340.             tmp = isALNUM(i);
  341.             }
  342.             else
  343.             tmp = isALNUM(regprev);    /* assume not alphanumeric */
  344.             while (s < strend) {
  345.                 i = *s;
  346.                 if (tmp != isALNUM(i))
  347.                     tmp = !tmp;
  348.                 else if (regtry(prog, s))
  349.                     goto got_it;
  350.                 s++;
  351.             }
  352.             if ((minlen || !tmp) && regtry(prog,s))
  353.                 goto got_it;
  354.             break;
  355.         case ALNUM:
  356.             while (s < strend) {
  357.                 i = *s;
  358.                 if (isALNUM(i)) {
  359.                     if (tmp && regtry(prog, s))
  360.                         goto got_it;
  361.                     else
  362.                         tmp = doevery;
  363.                 }
  364.                 else
  365.                     tmp = 1;
  366.                 s++;
  367.             }
  368.             break;
  369.         case NALNUM:
  370.             while (s < strend) {
  371.                 i = *s;
  372.                 if (!isALNUM(i)) {
  373.                     if (tmp && regtry(prog, s))
  374.                         goto got_it;
  375.                     else
  376.                         tmp = doevery;
  377.                 }
  378.                 else
  379.                     tmp = 1;
  380.                 s++;
  381.             }
  382.             break;
  383.         case SPACE:
  384.             while (s < strend) {
  385.                 if (isSPACE(*s)) {
  386.                     if (tmp && regtry(prog, s))
  387.                         goto got_it;
  388.                     else
  389.                         tmp = doevery;
  390.                 }
  391.                 else
  392.                     tmp = 1;
  393.                 s++;
  394.             }
  395.             break;
  396.         case NSPACE:
  397.             while (s < strend) {
  398.                 if (!isSPACE(*s)) {
  399.                     if (tmp && regtry(prog, s))
  400.                         goto got_it;
  401.                     else
  402.                         tmp = doevery;
  403.                 }
  404.                 else
  405.                     tmp = 1;
  406.                 s++;
  407.             }
  408.             break;
  409.         case DIGIT:
  410.             while (s < strend) {
  411.                 if (isDIGIT(*s)) {
  412.                     if (tmp && regtry(prog, s))
  413.                         goto got_it;
  414.                     else
  415.                         tmp = doevery;
  416.                 }
  417.                 else
  418.                     tmp = 1;
  419.                 s++;
  420.             }
  421.             break;
  422.         case NDIGIT:
  423.             while (s < strend) {
  424.                 if (!isDIGIT(*s)) {
  425.                     if (tmp && regtry(prog, s))
  426.                         goto got_it;
  427.                     else
  428.                         tmp = doevery;
  429.                 }
  430.                 else
  431.                     tmp = 1;
  432.                 s++;
  433.             }
  434.             break;
  435.         }
  436.     }
  437.     else {
  438.         if (minlen)
  439.             dontbother = minlen - 1;
  440.         strend -= dontbother;
  441.         /* We don't know much -- general case. */
  442.         do {
  443.             if (regtry(prog, s))
  444.                 goto got_it;
  445.         } while (s++ < strend);
  446.     }
  447.  
  448.     /* Failure. */
  449.     goto phooey;
  450.  
  451.     got_it:
  452.     prog->subbeg = strbeg;
  453.     prog->subend = strend;
  454.     if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
  455.         strend += dontbother;    /* uncheat */
  456.         if (safebase)            /* no need for $digit later */
  457.             s = strbeg;
  458.         else if (strbeg != prog->subbase) {
  459.             i = strend - string + (stringarg - strbeg);
  460.             s = nsavestr(strbeg,i);    /* so $digit will work later */
  461.             if (prog->subbase)
  462.                 Safefree(prog->subbase);
  463.             prog->subbeg = prog->subbase = s;
  464.             prog->subend = s+i;
  465.         }
  466.         else {
  467.             i = strend - string + (stringarg - strbeg);
  468.             prog->subbeg = s = prog->subbase;
  469.             prog->subend = s+i;
  470.         }
  471.         s += (stringarg - strbeg);
  472.         for (i = 0; i <= prog->nparens; i++) {
  473.             if (prog->endp[i]) {
  474.                 prog->startp[i] = s + (prog->startp[i] - string);
  475.                 prog->endp[i] = s + (prog->endp[i] - string);
  476.             }
  477.         }
  478.         if (prog->do_folding)
  479.             Safefree(string);
  480.     }
  481.     return(1);
  482.  
  483.     phooey:
  484.     if (prog->do_folding)
  485.         Safefree(string);
  486.     return(0);
  487. }
  488.  
  489. /*
  490.  - regtry - try match at specific point
  491.  */
  492. static int            /* 0 failure, 1 success */
  493. regtry(prog, string)
  494. regexp *prog;
  495. char *string;
  496. {
  497.     register int i;
  498.     register char **sp;
  499.     register char **ep;
  500.  
  501.     reginput = string;
  502.     regstartp = prog->startp;
  503.     regendp = prog->endp;
  504.     reglastparen = &prog->lastparen;
  505.     prog->lastparen = 0;
  506.  
  507.     sp = prog->startp;
  508.     ep = prog->endp;
  509.     if (prog->nparens) {
  510.         for (i = prog->nparens; i >= 0; i--) {
  511.             *sp++ = NULL;
  512.             *ep++ = NULL;
  513.         }
  514.     }
  515.     if (regmatch(prog->program + 1) && reginput >= regtill) {
  516.         prog->startp[0] = string;
  517.         prog->endp[0] = reginput;
  518.         return(1);
  519.     } else
  520.         return(0);
  521. }
  522.  
  523. /*
  524.  - regmatch - main matching routine
  525.  *
  526.  * Conceptually the strategy is simple:  check to see whether the current
  527.  * node matches, call self recursively to see whether the rest matches,
  528.  * and then act accordingly.  In practice we make some effort to avoid
  529.  * recursion, in particular by going through "ordinary" nodes (that don't
  530.  * need to know whether the rest of the match failed) by a loop instead of
  531.  * by recursion.
  532.  */
  533. /* [lwall] I've hoisted the register declarations to the outer block in order to
  534.  * maybe save a little bit of pushing and popping on the stack.  It also takes
  535.  * advantage of machines that use a register save mask on subroutine entry.
  536.  */
  537. static int            /* 0 failure, 1 success */
  538. regmatch(prog)
  539. char *prog;
  540. {
  541.     register char *scan;    /* Current node. */
  542.     char *next;        /* Next node. */
  543.     register int nextchar;
  544.     register int n;        /* no or next */
  545.     register int ln;        /* len or last */
  546.     register char *s;    /* operand or save */
  547.     register char *locinput = reginput;
  548.  
  549.     nextchar = *locinput;
  550.     scan = prog;
  551. #ifdef DEBUGGING
  552.     if (scan != NULL && regnarrate)
  553.         fprintf(stderr, "%s(\n", regprop(scan));
  554. #endif
  555.     while (scan != NULL) {
  556. #ifdef DEBUGGING
  557.         if (regnarrate)
  558.             fprintf(stderr, "%s...\n", regprop(scan));
  559. #endif
  560.  
  561. #ifdef REGALIGN
  562.         next = scan + NEXT(scan);
  563.         if (next == scan)
  564.             next = NULL;
  565. #else
  566.         next = regnext(scan);
  567. #endif
  568.  
  569.         switch (OP(scan)) {
  570.         case BOL:
  571.             if (locinput == regbol ? regprev == '\n' :
  572.                 ((nextchar || locinput < regeol) &&
  573.                   locinput[-1] == '\n') )
  574.             {
  575.                 /* regtill = regbol; */
  576.                 break;
  577.             }
  578.             return(0);
  579.         case EOL:
  580.             if ((nextchar || locinput < regeol) && nextchar != '\n')
  581.                 return(0);
  582.             if (!multiline && regeol - locinput > 1)
  583.                 return 0;
  584.             /* regtill = regbol; */
  585.             break;
  586.         case ANY:
  587.             if ((nextchar == '\0' && locinput >= regeol) ||
  588.               nextchar == '\n')
  589.                 return(0);
  590.             nextchar = *++locinput;
  591.             break;
  592.         case EXACTLY:
  593.             s = OPERAND(scan);
  594.             ln = *s++;
  595.             /* Inline the first character, for speed. */
  596.             if (*s != nextchar)
  597.                 return(0);
  598.             if (regeol - locinput < ln)
  599.                 return 0;
  600.             if (ln > 1 && bcmp(s, locinput, ln) != 0)
  601.                 return(0);
  602.             locinput += ln;
  603.             nextchar = *locinput;
  604.             break;
  605.         case ANYOF:
  606.             s = OPERAND(scan);
  607.             if (nextchar < 0)
  608.                 nextchar = UCHARAT(locinput);
  609.             if (s[nextchar >> 3] & (1 << (nextchar&7)))
  610.                 return(0);
  611.             if (!nextchar && locinput >= regeol)
  612.                 return 0;
  613.             nextchar = *++locinput;
  614.             break;
  615.         case ALNUM:
  616.             if (!nextchar)
  617.                 return(0);
  618.             if (!isALNUM(nextchar))
  619.                 return(0);
  620.             nextchar = *++locinput;
  621.             break;
  622.         case NALNUM:
  623.             if (!nextchar && locinput >= regeol)
  624.                 return(0);
  625.             if (isALNUM(nextchar))
  626.                 return(0);
  627.             nextchar = *++locinput;
  628.             break;
  629.         case NBOUND:
  630.         case BOUND:
  631.             if (locinput == regbol)    /* was last char in word? */
  632.                 ln = isALNUM(regprev);
  633.             else 
  634.                 ln = isALNUM(locinput[-1]);
  635.             n = isALNUM(nextchar); /* is next char in word? */
  636.             if ((ln == n) == (OP(scan) == BOUND))
  637.                 return(0);
  638.             break;
  639.         case SPACE:
  640.             if (!nextchar && locinput >= regeol)
  641.                 return(0);
  642.             if (!isSPACE(nextchar))
  643.                 return(0);
  644.             nextchar = *++locinput;
  645.             break;
  646.         case NSPACE:
  647.             if (!nextchar)
  648.                 return(0);
  649.             if (isSPACE(nextchar))
  650.                 return(0);
  651.             nextchar = *++locinput;
  652.             break;
  653.         case DIGIT:
  654.             if (!isDIGIT(nextchar))
  655.                 return(0);
  656.             nextchar = *++locinput;
  657.             break;
  658.         case NDIGIT:
  659.             if (!nextchar && locinput >= regeol)
  660.                 return(0);
  661.             if (isDIGIT(nextchar))
  662.                 return(0);
  663.             nextchar = *++locinput;
  664.             break;
  665.         case REF:
  666.             n = ARG1(scan);  /* which paren pair */
  667.             s = regmystartp[n];
  668.             if (!s)
  669.                 return(0);
  670.             if (!regmyendp[n])
  671.                 return(0);
  672.             if (s == regmyendp[n])
  673.                 break;
  674.             /* Inline the first character, for speed. */
  675.             if (*s != nextchar)
  676.                 return(0);
  677.             ln = regmyendp[n] - s;
  678.             if (locinput + ln > regeol)
  679.                 return 0;
  680.             if (ln > 1 && bcmp(s, locinput, ln) != 0)
  681.                 return(0);
  682.             locinput += ln;
  683.             nextchar = *locinput;
  684.             break;
  685.  
  686.         case NOTHING:
  687.             break;
  688.         case BACK:
  689.             break;
  690.         case OPEN:
  691.             n = ARG1(scan);  /* which paren pair */
  692.             reginput = locinput;
  693.  
  694.             regmystartp[n] = locinput;    /* for REF */
  695.             if (regmatch(next)) {
  696.                 /*
  697.                  * Don't set startp if some later
  698.                  * invocation of the same parentheses
  699.                  * already has.
  700.                  */
  701.                 if (regstartp[n] == NULL)
  702.                     regstartp[n] = locinput;
  703.                 return(1);
  704.             } else
  705.                 return(0);
  706.             /* NOTREACHED */
  707.         case CLOSE: {
  708.                 n = ARG1(scan);  /* which paren pair */
  709.                 reginput = locinput;
  710.  
  711.                 regmyendp[n] = locinput;    /* for REF */
  712.                 if (regmatch(next)) {
  713.                     /*
  714.                      * Don't set endp if some later
  715.                      * invocation of the same parentheses
  716.                      * already has.
  717.                      */
  718.                     if (regendp[n] == NULL) {
  719.                         regendp[n] = locinput;
  720.                         if (n > *reglastparen)
  721.                             *reglastparen = n;
  722.                     }
  723.                     return(1);
  724.                 } else
  725.                     return(0);
  726.             }
  727.             /*NOTREACHED*/
  728.         case BRANCH: {
  729.                 if (OP(next) != BRANCH)        /* No choice. */
  730.                     next = NEXTOPER(scan);    /* Avoid recursion. */
  731.                 else {
  732.                     do {
  733.                         reginput = locinput;
  734.                         if (regmatch(NEXTOPER(scan)))
  735.                             return(1);
  736. #ifdef REGALIGN
  737.                         /*SUPPRESS 560*/
  738.                         if (n = NEXT(scan))
  739.                             scan += n;
  740.                         else
  741.                             scan = NULL;
  742. #else
  743.                         scan = regnext(scan);
  744. #endif
  745.                     } while (scan != NULL && OP(scan) == BRANCH);
  746.                     return(0);
  747.                     /* NOTREACHED */
  748.                 }
  749.             }
  750.             break;
  751.         case CURLY:
  752.             ln = ARG1(scan);  /* min to match */
  753.             n  = ARG2(scan);  /* max to match */
  754.             scan = NEXTOPER(scan) + 4;
  755.             goto repeat;
  756.         case STAR:
  757.             ln = 0;
  758.             n = 32767;
  759.             scan = NEXTOPER(scan);
  760.             goto repeat;
  761.         case PLUS:
  762.             /*
  763.              * Lookahead to avoid useless match attempts
  764.              * when we know what character comes next.
  765.              */
  766.             ln = 1;
  767.             n = 32767;
  768.             scan = NEXTOPER(scan);
  769.             repeat:
  770.             if (OP(next) == EXACTLY)
  771.                 nextchar = *(OPERAND(next)+1);
  772.             else
  773.                 nextchar = -1000;
  774.             reginput = locinput;
  775.             n = regrepeat(scan, n);
  776.             if (!multiline && OP(next) == EOL && ln < n)
  777.                 ln = n;            /* why back off? */
  778.             while (n >= ln) {
  779.                 /* If it could work, try it. */
  780.                 if (nextchar == -1000 || *reginput == nextchar)
  781.                     if (regmatch(next))
  782.                         return(1);
  783.                 /* Couldn't or didn't -- back up. */
  784.                 n--;
  785.                 reginput = locinput + n;
  786.             }
  787.             return(0);
  788.         case END:
  789.             reginput = locinput; /* put where regtry can find it */
  790.             return(1);    /* Success! */
  791.         default:
  792.             printf("%x %d\n",scan,scan[1]);
  793.             FAIL("regexp memory corruption");
  794.         }
  795.  
  796.         scan = next;
  797.     }
  798.  
  799.     /*
  800.      * We get here only if there's trouble -- normally "case END" is
  801.      * the terminating point.
  802.      */
  803.     FAIL("corrupted regexp pointers");
  804.     /*NOTREACHED*/
  805. #ifdef lint
  806.     return 0;
  807. #endif
  808. }
  809.  
  810. /*
  811.  - regrepeat - repeatedly match something simple, report how many
  812.  */
  813. /*
  814.  * [This routine now assumes that it will only match on things of length 1.
  815.  * That was true before, but now we assume scan - reginput is the count,
  816.  * rather than incrementing count on every character.]
  817.  */
  818. static int
  819. regrepeat(p, max)
  820. char *p;
  821. int max;
  822. {
  823.     register char *scan;
  824.     register char *opnd;
  825.     register int c;
  826.     register char *loceol = regeol;
  827.  
  828.     scan = reginput;
  829.     if (max != 32767 && max < loceol - scan)
  830.         loceol = scan + max;
  831.     opnd = OPERAND(p);
  832.     switch (OP(p)) {
  833.     case ANY:
  834.         while (scan < loceol && *scan != '\n')
  835.             scan++;
  836.         break;
  837.     case EXACTLY:        /* length of string is 1 */
  838.         opnd++;
  839.         while (scan < loceol && *opnd == *scan)
  840.             scan++;
  841.         break;
  842.     case ANYOF:
  843.         c = UCHARAT(scan);
  844.         while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
  845.             scan++;
  846.             c = UCHARAT(scan);
  847.         }
  848.         break;
  849.     case ALNUM:
  850.         while (scan < loceol && isALNUM(*scan))
  851.             scan++;
  852.         break;
  853.     case NALNUM:
  854.         while (scan < loceol && !isALNUM(*scan))
  855.             scan++;
  856.         break;
  857.     case SPACE:
  858.         while (scan < loceol && isSPACE(*scan))
  859.             scan++;
  860.         break;
  861.     case NSPACE:
  862.         while (scan < loceol && !isSPACE(*scan))
  863.             scan++;
  864.         break;
  865.     case DIGIT:
  866.         while (scan < loceol && isDIGIT(*scan))
  867.             scan++;
  868.         break;
  869.     case NDIGIT:
  870.         while (scan < loceol && !isDIGIT(*scan))
  871.             scan++;
  872.         break;
  873.     default:        /* Oh dear.  Called inappropriately. */
  874.         FAIL("internal regexp foulup");
  875.         /* NOTREACHED */
  876.     }
  877.  
  878.     c = scan - reginput;
  879.     reginput = scan;
  880.  
  881.     return(c);
  882. }
  883.  
  884. /*
  885.  - regnext - dig the "next" pointer out of a node
  886.  *
  887.  * [Note, when REGALIGN is defined there are two places in regmatch()
  888.  * that bypass this code for speed.]
  889.  */
  890. char *
  891. regnext(p)
  892. register char *p;
  893. {
  894.     register int offset;
  895.  
  896.     if (p == ®dummy)
  897.         return(NULL);
  898.  
  899.     offset = NEXT(p);
  900.     if (offset == 0)
  901.         return(NULL);
  902.  
  903. #ifdef REGALIGN
  904.     return(p+offset);
  905. #else
  906.     if (OP(p) == BACK)
  907.         return(p-offset);
  908.     else
  909.         return(p+offset);
  910. #endif
  911. }
  912.